package com.spirng.leetcode.day06;

import java.util.ArrayList;
import java.util.List;

public class Test144 {
    public static void main(String[] args) {

    }
    public List<Integer> preorderTraversal(TreeNode root) {
        ArrayList<Integer> list = new ArrayList<>();
         preorder(list,root);
         return list;
    }
    public void preorder(List list,TreeNode treeNode){
        if(treeNode==null){
            return ;
        }
        list.add(treeNode.val);
        preorder(list,treeNode.left);
        preorder(list,treeNode.right);
    }
    public List<Integer> postorderTraversal(TreeNode root) {
        ArrayList<Integer> list = new ArrayList<>();
        postorder(list,root);
        return list;
    }
    public void postorder(List list,TreeNode treeNode){
        if(treeNode==null){
            return;
        }
        postorder(list,treeNode.left);
        postorder(list,treeNode.right);
        list.add(treeNode.val);
    }
    public List<Integer> inorderTraversal(TreeNode root) {
        ArrayList<Integer> list = new ArrayList<>();
        inorder(list,root);
        return list;
    }
    public void inorder(List list,TreeNode treeNode){
        if(treeNode==null){
            return;
        }
        inorder(list,treeNode.left);
        list.add(treeNode.val);
        inorder(list,treeNode.right);
    }
}
 class TreeNode {
      int val;
      TreeNode left;
     TreeNode right;
     TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
}